<template>
	<view class="maintenanceHome">
		<view :style="tabberStyle"
			class="maintenanceHome-search ff-bg-blue ff-padding-left-15 ff-padding-right-15 ff-padding-top-12 ff-padding-bottom-12">
			<u-search v-model="params.keyword" input-align="center" @change="projectNameChange" bg-color="#FFFFFF"
				:height="80" :show-action="false" :clearabled="true" placeholder="请输入要搜索的项目名称"></u-search>
		</view>
		<!-- 列表数据 -->
		<!-- 列表数据 -->
		<view>
			<view v-if="assessmentList.length > 0" class="maintenanceHome-content staff-top">
				<view class="maintenanceHome-content-box ff-bg-fff ff-margin-bottom-12 ff-border-radius-10"
					v-for="(item, index) of assessmentList" :key="index">
					<view class="ff-border-bottom ff-padding-15 ff-border-radius-10"
						@click.stop="assessmentClick(item)">
						<view class="ff-font-16 ff-color-333 ff-font-weight-600 ff-ellipsis-1 ff-line-height-1-2">
							{{ item.assessName }}
						</view>
						<view v-if="item.rescissionStatus == 1"
							class="rescindTips ff-padding-7 ff-border-radius-5 ff-color-fff ff-font-12 ff-line-height-1-5 ff-margin-top-9 ff-margin-bottom-5">
							请注意，社会单位已对本项目发起解约申请，点击进入项目详情查看与处理。如有异议，请尽快与单位联系。
						</view>
						<view class="ff-align-items-one ff-flex-wrap">
							<view class="ff-margin-right-10 ff-font-12">
								<view v-if="item.assessStatus == 2" class="tips1">评估状态&nbsp;|&nbsp;已响应</view>
								<view v-if="item.assessStatus == 3" class="tips1">评估状态&nbsp;|&nbsp;待指派</view>
								<view v-if="item.assessStatus == 11" class="tips1">评估状态&nbsp;|&nbsp;待评估</view>
								<view v-if="item.assessStatus == 4" class="tips1">评估状态&nbsp;|&nbsp;评估中</view>
								<view v-if="item.assessStatus == 5" class="tips1">评估状态&nbsp;|&nbsp;待提交</view>
								<view v-if="item.assessStatus == 6" class="tips1">评估状态&nbsp;|&nbsp;待审核</view>
								<view v-if="item.assessStatus == 7" class="tips1">评估状态&nbsp;|&nbsp;待签发</view>
								<view v-if="item.assessStatus == 8" class="tips1">评估状态&nbsp;|&nbsp;已驳回</view>
								<view v-if="item.assessStatus == 9" class="tips1">评估状态&nbsp;|&nbsp;已完成</view>
								<view v-if="item.assessStatus == 10" class="tips6">评估状态&nbsp;|&nbsp;已解约</view>
								<view v-if="item.assessStatus == 12" class="tips6">评估状态&nbsp;|&nbsp;已作废</view>
							</view>
							<view v-if="item.rescissionStatus == 1"
								class="tips4 ff-color-fff ff-font-12 ff-margin-right-10">
								解约确认中</view>
						</view>
						<view class="ff-margin--7" v-if="!item.modified"></view>
						<view class="ff-font-13 maintenanceHome-content-box-time" v-if="item.modified">
							更新于：{{ item.modified == null ? '/' : item.modified.substr(0, item.modified.length - 3) }}
						</view>
					</view>
					<view class="maintenanceHome-content-box-butList ff-padding-right-15 ff-justify-end">
						<view class="ff-padding-top-15 ff-padding-bottom-15"
							v-if="roleStatus == 2 && item.assessStatus == 3">
							<u-button type="primary" size="mini" @click.stop="assignClick(item, 1)">指派</u-button>
						</view>
						<view class="ff-padding-top-15 ff-padding-bottom-15"
							v-if="roleStatus == 2 && (item.assessStatus == 4 || item.assessStatus == 7 || item.assessStatus == 11)">
							<u-button type="primary" size="mini" @click.stop="assignClick(item, 2)">重新指派</u-button>
						</view>
						<view v-if="item.leaders.length > 0 || item.operators.length > 0">
							<view class="ff-padding-top-15 ff-padding-bottom-15"
								v-if="(isCheckOperators(userInfo.id, item.operators) || userInfo.id == item.leaders[0].userId) && (item.assessStatus == 4 || item.assessStatus == 11)">
								<u-button @click.stop="goAssessmentClick(item)" type="primary"
									size="mini">去评估</u-button>
							</view>
							<view class="ff-padding-top-15 ff-padding-bottom-15"
								v-if="item.assessStatus == 5 && isCheckOperators(userInfo.id, item.operators)">
								<u-button type="primary" size="mini"
									@click.stop="editReportClick(item)">填写评估报告</u-button>
							</view>
						</view>
						<view class="ff-padding-top-15 ff-padding-bottom-15"
							v-if="item.assessStatus == 6 || item.assessStatus == 7 || item.assessStatus == 8">
							<u-button @click.stop="reportClick(item)" type="primary" size="mini">评估报告书</u-button>
						</view>
					</view>
				</view>
				<u-divider v-if="noData" border-color="#909399" height="120" margin-bottom="30" color="#909399"
					bg-color="#f5f5f5">没有更多了</u-divider>
			</view>
			<view v-else>
				<f-noData v-if="roleStatus == 2" :topStyle.sync="topStyleAdmin"></f-noData>
				<f-noData v-else :topStyle.sync="topStyleStaff"></f-noData>
			</view>
		</view>
		<u-toast ref="uToast" />
		<f-loading ref="loading" />
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		name: 'companyProject',
		data() {
			return {
				tipsModel: false, // 提示弹窗
				tipsContent: '',
				assessmentList: [], // 机构列表数据 // 机构列表数据请求的参数，评估项目状态( 4.评估中 3.待指派 11.待评估5.待提交 6.待审核 7.待签发 9.已完成8.已驳回  10.已解约 )
				params: {
					current: 1,
					size: 15,
					statuses: '', // 4.评估中 3.待指派 11.待评估5.待提交 6.待审核 7.待签发 9.已完成 8.已驳回 10.已解约
					keyword: '',
					isUserTask: 0, // 是否是我的任务 0：否 1：是
				},
				noData: false // 没有更多了
			};
		},
		mounted() {
			this.showLoading() // 获取的机构项目数据列表
		},
		computed: {
			tabberStyle() {
				return `top:${this.customBar}px;`;
			},
			// 用户角色状态
			...mapState({
				roleStatus: modules => modules.infoList.roleStatus,
				userInfo: modules => modules.infoList
					.userInfo
			})
		},
		methods: {
			// onShow监听刷新
			showLoading() {
				this.params.current = 1;
				if (this.roleStatus == 2) {
					this.params.isUserTask = 0 // 获取管理员---机构项目数据列表
				} else {
					this.params.isUserTask = 1 // 获取员工---单位项目的数据列表
				}
				this.getMechanismList(); // 获取的机构项目数据列表
			},
			// 上拉加载
			pullLoading() {
				if (!this.noData) {
					this.params.current++;
					if (this.roleStatus == 2) {
						this.isUserTask = 0 // 获取管理员---机构项目数据列表
					} else {
						this.isUserTask = 1 // 获取员工---单位项目的数据列表
					}
					this.getMechanismList(); // 获取的数据列表
				}
			},
			// 下拉加载
			downLoading() {
				let that = this;
				that.params.current = 1;
				if (this.roleStatus == 2) {
					this.isUserTask = 0 // 获取管理员---机构项目数据列表
				} else {
					this.isUserTask = 1 // 获取员工---单位项目的数据列表
				}
				that.getMechanismList(); // 获取的数据列表
				setTimeout(() => {
					that.$refs.uToast.show({
						title: '刷新成功',
						type: 'success'
					});
					uni.stopPullDownRefresh(); // 停止刷新
				}, 100);
			},
			// 搜索项目
			projectNameChange(e) {
				// console.log('当前搜索的值----', this.keyword)
				this.params.current = 1;
				if (this.roleStatus == 2) {
					this.isUserTask = 0 // 获取管理员---机构项目数据列表
				} else {
					this.isUserTask = 1 // 获取员工---单位项目的数据列表
				}
				this.getMechanismList(); // 获取单位的列表数据
			},
			// 获取机构的列表数据(管理员or员工)
			async getMechanismList() {
				this.$refs.loading.openLoading('加载中···'); // 打开loading动画
				const data = await this.$http.post({
					url: '/bsAssessProjectInfo/project/home/page',
					data: this.params
				});
				// console.log('获取的机构列表数据------', data);
				if (data.code == 200) {
					this.$refs.loading.closeLoading(); // 关闭loading动画
					const infoList = data.data.records;
					if (infoList.length == 0 && this.params.current == 1) {
						// console.log('首次加载没数据');
						this.noData = false;
						this.assessmentList = [];
					} else if (infoList.length < 3 && this.params.current == 1) {
						// console.log('首次加载有数据，但少于4条');
						this.noData = true;
						this.assessmentList = infoList;
					} else if (infoList.length !== 0 && this.params.current == 1) {
						// console.log('首次加载有数据');
						this.noData = false;
						this.assessmentList = infoList;
					} else if (infoList.length !== 0 && this.params.current > 1) {
						// console.log('上拉加载更多数据');
						this.noData = false;
						this.assessmentList = this.assessmentList.concat(infoList);
					} else if (infoList.length == 0 && this.params.current > 1) {
						// console.log('上拉加载没有更多数据了');
						this.noData = true;
					}
				} else {
					this.$refs.loading.closeLoading(); // 关闭loading动画
					this.$refs.uToast.show({
						title: data.msg,
						type: 'error'
					});
				}
			},
			// 循环比较执业人员是否有权限编辑报告书
			isCheckOperators(curId, recordId) {
				// console.log(curId, recordId);
				for (let i = 0; i < recordId.length; i++) {
					if (curId == recordId[i].userId) {
						return true;
					}
				}
				return false;
			},
			// 点击项目列表进入详情
			assessmentClick(item) {
				this.$emit('handleScroll') // 记录当前点击列表的数据位置
				this.$pageJump.navigateTo(
					'/pages/assessment/mechanism/home/components/details/projectMaintenanceRecord?id=' +
					item.assessId);
			},
			// 指派 or 重新指派
			assignClick(item, type) {
				console.log('指派 or 重新指派', item, type)
				// hasCorrected 是否已勘误:1.未勘误 2.已勘误
				if (item.hasCorrected == 1) {
					// 未勘误---去勘误
					this.$pageJump.navigateTo(
						'/pages/assessment/mechanism/home/components/corrigendum/Corrigendum?assessId=' +
						item.assessId);
				} else {
					if (item.assessStatus == 7) {
						// 重新指派技术负责人
						this.$pageJump.navigateTo('/pages/assessment/mechanism/home/components/assign/Assign?assessId=' +
							item
							.assessId + '&type=' + type + '&status=' + 3);
					} else {
						// 已勘误---去指派
						this.$pageJump.navigateTo('/pages/assessment/mechanism/home/components/assign/Assign?assessId=' +
							item
							.assessId + '&type=' + type + '&status=' + 2);
					}
				}
			},
			// 去评估
			async goAssessmentClick(item) {
				//检测操作人员证书是否到期
				const data = await this.$http.post({
					url: '/bsAssessProjectInfo/checkForCertificate?assessId=' + item.assessId
				})
				// console.log('检查证书是否到期', data)
				if (data.code == 200) {
					if (data.data.certificateInValid && data.data.leader) {
						uni.showModal({
							title: '证书已到期',
							content: data.data.msg,
							confirmColor: '#3178FF',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定')
								}
							},
						})
					} else {
						// 跳转到去评估的页面
						this.$pageJump.navigateTo(
							'/pages/assessment/mechanism/home/components/mechanismWork/mechanismWorking?assessId=' +
							item
							.assessId);
					}
				} else {
					uni.showModal({
						title: '温馨提示',
						content: data.msg,
						confirmColor: '#3178FF',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								// console.log('用户点击确定')
							}
						},
					})
				}
			},
			// 填写报告书
			editReportClick(item) {
				this.$pageJump.navigateTo('/pages/assessment/mechanism/home/components/details/editReport?id=' + item
					.assessId);
			},
			// 评估报告书（查看以及操作）
			reportClick(item) {
				this.$pageJump.navigateTo('/pages/assessment/mechanism/home/components/details/Report?id=' + item
					.assessId);
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import '@/pages/assessment/mechanism/home/components/maintenanceHome.scss';

	.staff-top {
		margin: 150rpx 30rpx 130rpx;
	}

	.maintenanceHome-search {
		position: fixed;
		left: 0;
		z-index: 9;
		width: 100%;
	}

	.tips6 {
		background-color: #a8a7b5;
		color: #fff;
		padding: 12rpx 15rpx;
		border-radius: 8rpx;
		margin-top: 8.5rpx;
		margin-bottom: 8.5rpx;
	}

	.rescindTips {
		background-color: #ffebeb;
		color: #ff0000;
	}
</style>